package com.ocom.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static org.springframework.beans.BeanUtils.copyProperties;

public class ObjectList {

    /**
     * Object 对象转 List
     */
    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
    public static <T, S> List<S> copyList(List<T> sources, Supplier<S> target) {
        if (sources == null || target == null) {
            return null;
        }
        return sources.stream().map(
                source -> {
                    S s = target.get();
                    copyProperties(source, s);
                    return s;
                }
        ).collect(Collectors.toList());
    }
}
